package com.example.demo.service.impl;

import com.example.demo.entity.DAccessory;
import com.example.demo.entity.PPhoto;
import com.example.demo.mapper.PPhotoMapper;
import com.example.demo.service.FileService;
import com.example.demo.service.PPhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.example.demo.exception.bizException.BizException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-10-09
*/
@Slf4j
@Service
public class PPhotoServiceImpl extends ServiceImpl<PPhotoMapper, PPhoto> implements PPhotoService {

    @Autowired
    private FileService fileService;

    @Override
    public Page<PPhoto> listPPhotosByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询pPhoto: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<PPhoto> queryWrapper =  new QueryWrapper<PPhoto>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<PPhoto> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询pPhoto完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public PPhoto getPPhotoById(int id) {
        log.info("正在查询pPhoto中id为{}的数据",id);
        PPhoto pPhoto = super.getById(id);
        log.info("查询id为{}的pPhoto{}",id,(null == pPhoto?"无结果":"成功"));
        return pPhoto;
    }

    @Override
    public List<PPhoto> getPPhotoByPId(int pId) {
        log.info("正在查询专利图片中pid为{}的数据",pId);
        List<PPhoto> list=super.list(new QueryWrapper<PPhoto>().eq("p_id",pId));
        log.info("查询pId为{}的图片有{}个",pId,list.size());
        return list;
    }

    @Override
    public int insertPPhoto(PPhoto pPhoto) {
        log.info("正在插入pPhoto");
        if (super.save(pPhoto)) {
            log.info("插入pPhoto成功,id为{}",pPhoto.getPhotoName());
            return pPhoto.getPhotoId();
        } else {
            log.error("插入pPhoto失败");
            throw new BizException("添加失败");
        }
    }

    @Transactional
    @Override
    public int deletePPhotoById(int id) {
        log.info("正在删除id为{}的pPhoto",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的pPhoto成功",id);
            return id;
        } else {
            log.error("删除id为{}的pPhoto失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updatePPhoto(PPhoto pPhoto) {
        log.info("正在更新id为{}的pPhoto",pPhoto.getPhotoId());
        if (super.updateById(pPhoto)) {
            log.info("更新d为{}的pPhoto成功",pPhoto.getPhotoId());
            return pPhoto.getPhotoId();
        } else {
            log.error("更新id为{}的pPhoto失败",pPhoto.getPhotoId());
            throw new BizException("更新失败[id=" + pPhoto.getPhotoId() + "]");
        }
    }

}
